package com.verisette.ncos.service.master.bizunit;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;

import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;
import com.verisette.ncos.persistence.dao.AccessControlDAO;
import com.verisette.ncos.persistence.dao.UserAccessLogDAO;
import com.verisette.ncos.persistence.domain.CustomMasterUserGroup;
import com.verisette.ncos.persistence.domain.LogUserAccess;
import com.verisette.ncos.persistence.domain.MasterUserGroup;
import com.verisette.ncos.persistence.domain.MasterUserProfile;

public class ViewAccessLogUserProfileBU extends CommonBusinessUnit {

	private Logger logger = Logger.getLogger(ViewAccessLogUserProfileBU.class);
	
	private UserAccessLogDAO userAccessLogDAO;
	@Autowired
	@Required
	public void setUserAccessLogDAO(UserAccessLogDAO userAccessLogDAO) {
		this.userAccessLogDAO = userAccessLogDAO;
	}
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute" );
		
		MasterUserProfile userProfile = (MasterUserProfile)processContext.getValueObject();
		List<LogUserAccess> logUserAccessList = null;
		List<MasterUserGroup> totalUserGroupList = null;
		LogUserAccess criteria = new LogUserAccess();
		
		
		criteria.setUspId(userProfile.getUspId());
		
		// Inquiry Access Log in LOG_USER_ACCESS
		logger.debug("1.Inquiry Access Log in LOG_USER_ACCESS.");
		logger.debug("--> uspId : " + userProfile.getUspId().intValue());
		logUserAccessList = (List<LogUserAccess>)commonDAO.findByExample(criteria, processContext.getPaging());
		
		logger.debug("--> total Logs : " + logUserAccessList.size());
		
//		// Inquiry Total User Group for mapping usgName
//		logger.debug("2.Inquiry Total User Group for mapping usgName");
//		totalUserGroupList = (List<MasterUserGroup>)commonDAO.findAll(MasterUserGroup.class);
//		
//		logger.debug("--> total Groups : " + totalUserGroupList.size());
		
		
		//Add by thippawan @2010-10-06
		List<CustomMasterUserGroup> listUserGroup = userAccessLogDAO.listUserGroupByUser( String.valueOf(userProfile.getUspId()) );
		StringBuffer usgName;
		if(listUserGroup.size()!=0){
			for(LogUserAccess logUser : logUserAccessList){
				usgName = new StringBuffer();
				for(CustomMasterUserGroup userGroup : listUserGroup){
					usgName.append(userGroup.getUsgName());
					usgName.append(",");
				}
				// remove last index of ','
				int lastIndex = usgName.length()-1;
				usgName.deleteCharAt(lastIndex);
				logUser.setUsgName(usgName.toString());
			}
		}
		
		
		// Mapping MasterLogUserAccess to User Group
		logger.debug("3.Mapping MasterLogUserAccess to User Group");
		/*	Comment by thippawan @20101004 :- usgId @MasterUserGroup has been removed.
		for(LogUserAccess logUser : logUserAccessList){
			for(MasterUserGroup userGroup : totalUserGroupList){
				if(logUser.getUsgId().intValue() == userGroup.getUsgId().intValue()){
					logUser.setUsgName(userGroup.getUsgName());
					break;
					
				}
				
			}
			
		}
		*/
		processContext.setValueObject(logUserAccessList);
		
	}
	
}
